//Script by Pogoman from Chernarus Life Revolution, edited and used for Sahrani Life Reloaded by MAD T (year 2010)
if (clone) exitWith {player groupChat "Your already wearing clothes"};

private ["_type", "_ar", "_oldUnit", "_newUnit", "_dummyUnit", "_dummyGroup", "_fSelectWeapon"];

_type = _this select 0;
_oldtype = typeof player;
_oldUnit = player;

if (_type == "Terrorist") then 
{
clone_class = clone_random_class select random 8;
};
if (_type == "Mayor") then 
{
clone_class = "OfficerE";
};
if (_type == "Normal") exitWith 
{
	call compile format['
	addSwitchableUnit old%1;
	selectPlayer old%1;
	%1 setdamage 1;
	old%1 setpos getpos %1;
	%1 setpos getpos abstellkammer;
	monster = %1;
	old%1 setvehicleinit "
	%1 = this;
	this setvehiclevarname ""%1"";
	";
	processinitcommands;
	old%1 = objnull;
	', rolestring];
	[player] joinsilent startgroup;  startgroup selectLeader player;
	statdialogaction = player addAction [localize "STRS_dead_menuentry_hints", "openDialogs2.sqf", ["spielerliste"]];	
action_inventar = player addAction [localize "STRS_dead_menuentry_inventar", "invOpen.sqf", []]; 
playanimationaction = player addAction [localize "STRS_dead_menuentry_animation", "animDlgOpen.sqf", []];
if (iHaveaCellphone) then {
actionCellPhoneMenu = player addAction [format[localize "STRS_addAction_cellphonemenu"], "openDialogs2.sqf",["cellphonemenu"]];};
clone = false;
sleep 60;
deletevehicle monster;
};

clone = true;
_ar = [weapons _oldUnit, magazines _oldUnit, rank _oldUnit, score _oldUnit, group _oldUnit, getPos _oldUnit, (leader _oldUnit) == _oldUnit, vehicleVarName _oldUnit];

_fSelectWeapon = {
	private ["_unit", "_weap", "_cfg", "_muz", "_ar"];
	_unit = _this select 0;
	_weap = _this select 1;
	_cfg = (configFile >> "CfgWeapons" >> _weap >> "muzzles");
	if (isArray _cfg) then {
		_ar = getArray _cfg;
		_muz = _ar select 0;
		if (_muz == "this") then { _muz = _weap };
	} else {
		_muz = _weap;
	};

	if (player hasWeapon _weap) then { _unit selectWeapon _muz };
};

_dummyGroup = createGroup (side _oldUnit);
_dummyUnit = (_ar select 4) createUnit [clone_class, [0,0,0], [], 0, "NONE"]; // Join in the old group incase there was only 1 member
[_oldUnit] join _dummyGroup;

_newUnit = _dummyGroup createUnit [clone_class, _ar select 5, [], 0, "NONE"];

addSwitchableUnit _newUnit;
titleCut ["","black faded", 0];
player setpos getpos abstellkammer;
sleep 1;

_oldUnit setVehicleInit format["this setVehicleVarName 'old%1'; old%1 = this", _ar select 7];
_newUnit setVehicleInit format["this setVehicleVarName '%1'; %1 = this;", _ar select 7];
processInitCommands;

sleep 1;

player removeaction actionCellPhoneMenu;
player removeAction playanimationaction;
player removeAction statdialogaction;
player removeAction action_inventar;

selectPlayer _newUnit;
removeAllWeapons _newUnit;
{ _newUnit addMagazine _x } forEach (_ar select 1);
{ _newUnit addWeapon _x } forEach (_ar select 0);

if ((primaryWeapon _newUnit) != "") then { [_newUnit, primaryWeapon _newUnit] call _fSelectWeapon };

if (iHaveaCellphone) then {
		actionCellPhoneMenu = player addAction [format[localize "STRS_addAction_cellphonemenu"], "openDialogs2.sqf",["cellphonemenu"]];};

[_oldUnit] joinsilent (group server);
[_newUnit] joinsilent startgroup;
startgroup selectLeader _newUnit;
deleteVehicle _dummyUnit;
deletegroup _dummyGroup;

removeSwitchableUnit _newUnit;
removeSwitchableUnit _oldUnit;

statdialogaction = player addAction [localize "STRS_dead_menuentry_hints", "openDialogs2.sqf", ["spielerliste"]];	
action_inventar = player addAction [localize "STRS_dead_menuentry_inventar", "invOpen.sqf", []]; 
playanimationaction = player addAction [localize "STRS_dead_menuentry_animation", "animDlgOpen.sqf", []];

player addEventHandler ["fired", {["fired", (_this select 4), (_this select 1)] execVM "stun.sqf";}];
player setpos (_ar select 5);

titleCut["", "BLACK in",2];